package com.happly.happlycloud.controller.backend;

import com.happly.happlycloud.common.Const;
import com.happly.happlycloud.common.ServerResponse;
import com.happly.happlycloud.pojo.User;
import com.happly.happlycloud.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/manage/user")
@Api("用户管理后台")
public class UserManageController {

    @Autowired
    private IUserService iUserService;

    @RequestMapping(value="login.do",method = RequestMethod.POST)
    @ApiOperation("添加")
    public ServerResponse<User> login(String username, String password, HttpSession session, HttpServletResponse httpServletResponse){
        ServerResponse<User> response = iUserService.login(username,password);
        if(response.isSuccess()){
            User user = response.getData();
            if(user.getRole() == Const.Role.ROLE_ADMIN){
                //CookieUtil.writeLoginToken(httpServletResponse,session.getId());

                //RedisShardedPoolUtil.setEx(session.getId(), JsonUtil.objToString(response.getData()),Const.RedisCacheExtime.REDIS_SESSION_EXTIME);
                //新增redis共享cookie session的方式
                session.setAttribute(Const.CURRENT_USER,response.getData());
               // CookieUtil.writeLoginToken(httpServletResponse,session.getId());
                //RedisShardedPoolUtil.setEx(session.getId(),JsonUtil.objToString(response),Const.RedisCacheExtime.REDIS_SESSION_EXTIME);
                return response;

            }else{
                return ServerResponse.createByErrorMessage("不是管理员,无法登录");
            }
        }
        return response;
    }

    @RequestMapping("list.do")
    @ApiOperation("添加")
    public ServerResponse getList(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        //填充业务
        return iUserService.getUserList(pageNum,pageSize);
    }

    @RequestMapping( "search.do")
    @ApiOperation("添加")
    public ServerResponse productSearch(String userName,Integer userId,String email,String phone, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,@RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        //填充业务
        return iUserService.searchUser(userName,userId,email,phone,pageNum,pageSize);
    }

    @RequestMapping( "set_user_role.do")
    @ApiOperation("添加")
    public ServerResponse changeRole(Integer userId,Integer role, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,@RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        //填充业务
        return iUserService.setUserRole(userId,role,pageNum,pageSize);
    }

    @RequestMapping( "frose_user.do")
    @ApiOperation("添加")
    public ServerResponse froseUser(Integer userId,@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,@RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        //填充业务
        return iUserService.frose(userId,pageNum,pageSize);
    }

    @RequestMapping( "unfrose_user.do")
    @ApiOperation("添加")
    public ServerResponse unfroseUser(Integer userId,@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,@RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        //填充业务
        return iUserService.unfrose(userId,pageNum,pageSize);
    }
    //修改过
    @RequestMapping("detail.do")
    @ApiOperation("添加")
    public ServerResponse getDetail( Integer userId){
        //填充业务
        return iUserService.manageUserDetail(userId);
    }

    @RequestMapping("finduser.do")
    @ApiOperation("添加")
    public ServerResponse getUser(Integer userId){
            //填充业务
            return iUserService.manageUserDetail(userId);
    }

    @RequestMapping("save.do")
    @ApiOperation("添加")
    public ServerResponse userSave(User userMessage){
        //填充我们增加产品的业务逻辑
        return iUserService.saveOrUpdateUser(userMessage);
    }

    @RequestMapping("get_home_count.do")
    @ApiOperation("添加")
    public ServerResponse getHomeCount(){
        //填充我们增加产品的业务逻辑
        return iUserService.getAllCount();
    }

}
